﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using NetDon.OA.Portal.HtmlTool;
using NetDon.Utilities;
using NetDon.Variation.Data;
using NetDon.Variation.Data.CollectionClasses;
using NetDon.Variation.Data.EntityClasses;
using NetDon.Variation.Data.HelperClasses;
using NetDon.eBoss.Data.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using Transaction = NetDon.Variation.Data.HelperClasses.Transaction;

namespace NetDon.OA.Portal.Product
{
    public class VariationProductService
    {
        public int GetVariationProductCount(VariationProudctCriteria criteria)
        {
            var collection = new VariationproductCollection();
            return
                int.Parse(
                    collection.GetScalar(VariationproductFieldIndex.Id, null, AggregateFunction.CountDistinct,
                                         GetVariationProductPredicate(criteria)).ToString());
        }
        public VariationproductCollection GetVariationProducts()
        {
            var collection = new VariationproductCollection();
            collection.GetMulti(null);
            return collection;

        }

        private IPredicate GetVariationProductPredicate(VariationProudctCriteria criteria)
        {
            var expression = new PredicateExpression();

            if (!string.IsNullOrEmpty(criteria.Name))
            {
                expression.Add(VariationproductFields.Name % ("%" + criteria.Name + "%"));
            }


            return expression;
        }

        public bool RemoveVariationProduct(int id)
        {
            var trans = new Transaction(IsolationLevel.ReadCommitted, "RemoveVariationProduct");
            try
            {
             

                var styles = new VariationproductskusCollection();
                styles.GetMulti(VariationproductskusFields.Variationproductid == id);
                foreach (var style in styles)
                {
                    style.Delete();
                    trans.Add(style);
                }
                var entity = new VariationproductEntity(id);
                trans.Add(entity);
                entity.Delete();
                trans.Commit();

            }
            catch (Exception)
            {
                trans.Rollback();

                throw;
            }
            return true;

        }

        public IEnumerable SearchVariationProductByCondition(VariationProudctCriteria criteria, string sortExpression,
                                                            System.Web.UI.WebControls.SortDirection sortDirection,
                                                            int pageSize, int pageIndex)
        {
            var sort = GetVariationProductSortExpression(sortExpression, sortDirection);
            var collection = new VariationproductCollection();
            collection.GetMulti(GetVariationProductPredicate(criteria), 0, sort,
                                null, pageIndex + 1, pageSize);


            return collection.Select(col => GetVariationProudctData(col));

          
        }
        private VariationProudctData GetVariationProudctData(VariationproductEntity entity)
        {
            var productSkuCollection = new VariationproductskusCollection();
            productSkuCollection.GetMulti(VariationproductskusFields.Variationproductid == entity.Id);
            var minStockQuatity = string.Empty;
            int quantity = 1;
            foreach (var productSku in productSkuCollection)
            {
                var productInfo = ProductXiaoSoftService.GetProductInfo(productSku.Subsku);
                if (string.IsNullOrWhiteSpace(productInfo.StockQuantity))
                {
                    continue;
                }
                if (string.IsNullOrWhiteSpace(minStockQuatity) )
                {
                    minStockQuatity = productInfo.StockQuantity;
                    quantity= productSku.Quantity??1;
                    continue;
                }
                 if (Convert.ToInt32(productInfo.StockQuantity) < Convert.ToInt32(minStockQuatity))
                {
                    minStockQuatity = productInfo.StockQuantity;
                      quantity= productSku.Quantity??1;
                }
             
            }

            return new VariationProudctData()
                       {
                           Id = entity.Id,
                           Sku =entity.Sku,
                           Name =entity.Name,
                           MinStockQuantity = string.IsNullOrEmpty(minStockQuatity) ? minStockQuatity : (Convert.ToInt32(minStockQuatity)/(quantity)).ToString()

                       };

        }
        private SortExpression GetVariationProductSortExpression(string strInput, SortDirection sortDirection)
        {
            SortExpression sort;
            switch (strInput)
            {
                case "Name":
                    sort = new SortExpression(VariationproductFields.Name | (SortOperator)(int)sortDirection);
                    break;
                case "Sku":
                    sort = new SortExpression(VariationproductFields.Sku | (SortOperator)(int)sortDirection);
                    break;

                default:
                    sort = new SortExpression
                               {
                                   VariationproductFields.Name | SortOperator.Ascending
                               };
                    sort.Add(new SortClause(VariationproductFields.Sku, SortOperator.Ascending));
                    break;

            }
            return sort;
        }


        #region VariationProductSKU

        public IEnumerable<VariationProudctSKUData> SearchVariationProductSKUs(int accountId)
        {

            var collection = new VariationproductskusCollection();
            var expression = new PredicateExpression();
            expression.Add(VariationproductskusFields.Variationproductid == accountId);
            collection.GetMulti(expression);
            return collection.Select(col => GetVariationProudctSKUData(col));



        }

        public bool RemoveVariationProductSKU(int id)
        {
            var trans = new Transaction(IsolationLevel.ReadCommitted, "RemoveVariationProductSKU");
            try
            {
                var entity = new VariationproductskusEntity(id);
                trans.Add(entity);
                entity.Delete();

                trans.Commit();

            }
            catch (Exception)
            {
                trans.Rollback();

                throw;
            }
            return true;

        }
        private VariationProudctSKUData GetVariationProudctSKUData(VariationproductskusEntity entity)
        {
            var productInfo = ProductXiaoSoftService.GetProductInfo(entity.Subsku);
            return new VariationProudctSKUData()
                       {
                           Id = entity.Id.ToString(),
                           Quantity = entity.Quantity,
                           Subsku = entity.Subsku,
                           GoodsName = productInfo.ProductName,
                           Spec = productInfo.Spec,
                           StockQuantity = productInfo.StockQuantity,
                           Variationproductid = entity.Variationproductid
                       };

        }
        #endregion

    }

    [Serializable]
    public class VariationProudctCriteria
    {
        public string Name { get; set; }

    }
    [Serializable]
    public class VariationProudctSKUData
    {
        public string Id { get; set; }

        public string Subsku { get; set; }

        public int? Quantity { get; set; }

        public string StockQuantity { get; set; }

        public string GoodsName { get; set; }

        public string Spec { get; set; }

        public int? Variationproductid { get; set; }
    }

    [Serializable]
    public class VariationProudctData
    {
        public int Id { get; set; }

        public string Name { get; set; }


        public string MinStockQuantity { get; set; }

        public string Sku { get; set; }

    }
}